﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using AbpZeroTemplate.Authorization;
using AbpZeroTemplate.Authorization.Users;
using AbpZeroTemplate.Authorization.Users.Dto;
using AbpZeroTemplate.Dto;
using AbpZeroTemplate.Web.Controllers;
using AbpZeroTemplate.Web.Swagger;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace AbpZeroTemplate.Api.Admin.Controllers
{
    /// <summary>用户管理
    /// </summary>
    [Area("Admin")]
    [Route("Admin/User/[action]")]
    [ApiName("Admin")]
    [RemoteService]
    [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users)]
    public class UserController : AbpZeroTemplateControllerBase
    {
        private readonly IUserAppService _userAppService;
        /// <summary>
        /// </summary>
        public UserController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        /// <summary>查询用户
        /// </summary>
        [HttpGet]
        public async Task<PagedResultDto<UserListDto>> GetUsers(GetUsersInput input)
        {
            return await _userAppService.GetUsers(input);
        }

        /// <summary>将用户导出成Excel
        /// </summary>
        [HttpGet]
        public async Task<FileDto> GetUsersToExcel()
        {
            return await _userAppService.GetUsersToExcel();
        }

        /// <summary>查询编辑的用户
        /// </summary>
        [HttpGet]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_Create, AppPermissions.Pages_Administration_Users_Edit)]
        public async Task<GetUserForEditOutput> GetUserForEdit([FromQuery]NullableIdDto<long> input)
        {
            return await _userAppService.GetUserForEdit(input);
        }

        /// <summary>获取用户权限
        /// </summary>
        [HttpGet]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_ChangePermissions)]
        public async Task<GetUserPermissionsForEditOutput> GetUserPermissionsForEdit(EntityDto<long> input)
        {
            return await _userAppService.GetUserPermissionsForEdit(input);
        }

        /// <summary>重置用户权限
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_ChangePermissions)]
        public async Task ResetUserSpecificPermissions([FromBody] EntityDto<long> input)
        {
            await _userAppService.ResetUserSpecificPermissions(input);
        }

        /// <summary>修改用户权限
        /// </summary>
        [HttpPut]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_ChangePermissions)]
        public async Task UpdateUserPermissions([FromBody]UpdateUserPermissionsInput input)
        {
            await _userAppService.UpdateUserPermissions(input);
        }

        /// <summary>创建或者修改用户
        /// </summary>
        [HttpPost]
        public async Task CreateOrUpdateUser([FromBody]CreateOrUpdateUserInput input)
        {
            await _userAppService.CreateOrUpdateUser(input);
        }

        /// <summary>删除用户
        /// </summary>
        [HttpDelete]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_Delete)]
        public async Task DeleteUser([FromBody]EntityDto<long> input)
        {
            await _userAppService.DeleteUser(input);
        }

        /// <summary>解锁
        /// </summary>
        [HttpPost]
        public async Task UnlockUser([FromBody] EntityDto<long> input)
        {
            await _userAppService.UnlockUser(input);
        }

        /// <summary>修改当前用户的语言
        /// </summary>
        //[HttpPost]
        //public async Task ChangeLanguage([FromBody]ChangeUserLanguageDto input)
        //{
        //    await _userAppService.ChangeLanguage(input);
        //}
    }
}
